package project.ccso.top.dao.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import project.ccso.top.dao.CommodityInfoDao;
import project.ccso.top.entity.CommodityInfo;

/**
 * 商品信息Dao的实现类
 * @author aibinxiao
 * @date 2018年8月23日 上午10:37:10
 */
public class CommodityInfoDaoImpl implements CommodityInfoDao {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(CommodityInfoDaoImpl.class);
	
	/*// 1.发布二货商品信息Dao实现类
	@Override
	public void saveCommodityInfo(CommodityInfo commodityInfo) throws Exception{
		// 存储二货商品信息
		this.getHibernateTemplate().saveOrUpdate(commodityInfo);
	}
	
	// 2.根据商品名模糊查询商品个数Dao实现类
	@Override
	public int findCountByName(String name) {
		int count = 0;
		String hql = "select count(*) from CommodityInfo where name like '%"+ name +"%'";
		List<Long> list = this.getHibernateTemplate().find(hql);
		if (list != null && list.size() > 0) {
			count = list.get(0).intValue();
			logger.info("关键词(" + name + ")有("+ count +")个符合要求的商品");
		}else{
			logger.info("关键词(" + name + ")没有符合要求的商品");
		}
		return count;
	}

	// 3.根据商品名模糊查询分页商品Dao实现类
	@Override
	public List<CommodityInfo> findPageByName(String name, int offset, int size) {
		List<CommodityInfo> result = null;
		String hql = "from CommodityInfo where name like ? order by issueTime desc";
		List<CommodityInfo> list = this.getHibernateTemplate().execute(new PageHibernateCallback<CommodityInfo>(hql, new Object[]{"%"+name+"%"}, offset, size));
		if (list != null && list.size() > 0) {
			result = list;
			logger.info("关键词(" + name + ")有("+ list.size() +")个符合要求的商品");
		} else{
			logger.info("关键词(" + name + ")没有找到符合要求的商品");
		}
		return result;
	}

	// 4.分页条件查询二货信息记录数量Dao实现类
	@Override
	public int countByParams(String id, String oneCategoryIdStr, String twoCategoryIdStr, String threeCategoryIdStr,
			String provinceIdStr, String cityIdStr, String schoolIdStr, String tradeWayStr, String reportStatusStr,
			String shieldStatusStr, String tradeStatusStr, String startDate, String endDate) {
		int count = 0;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(CommodityInfo.class);
		if(!StringUtils.isEmpty(id)){
			Integer commodityInfoId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(commodityInfoId));
		}
		if(!StringUtils.isEmpty(oneCategoryIdStr)){
			Integer oneCategoryId = Integer.valueOf(oneCategoryIdStr);
			criteria.add(Restrictions.eq("oneCategoryId", oneCategoryId));
		}
		if(!StringUtils.isEmpty(twoCategoryIdStr)){
			Integer twoCategoryId = Integer.valueOf(twoCategoryIdStr);
			criteria.add(Restrictions.eq("twoCategoryId", twoCategoryId));
		}
		if(!StringUtils.isEmpty(threeCategoryIdStr)){
			Integer threeCategoryId = Integer.valueOf(threeCategoryIdStr);
			criteria.add(Restrictions.eq("threeCategoryId", threeCategoryId));
		}
		if(!StringUtils.isEmpty(provinceIdStr)){
			Integer provinceId = Integer.valueOf(provinceIdStr);
			criteria.add(Restrictions.eq("provinceId", provinceId));
		}
		if(!StringUtils.isEmpty(cityIdStr)){
			Integer cityId = Integer.valueOf(cityIdStr);
			criteria.add(Restrictions.eq("cityId", cityId));
		}
		if(!StringUtils.isEmpty(schoolIdStr)){
			Integer schoolId = Integer.valueOf(schoolIdStr);
			criteria.add(Restrictions.eq("schoolId", schoolId));
		}	
		if(!StringUtils.isEmpty(tradeWayStr)){
			Integer tradeWay = Integer.valueOf(tradeWayStr);
			criteria.add(Restrictions.eq("tradeWay", tradeWay));
		}
		if(!StringUtils.isEmpty(reportStatusStr)){
			Integer reportStatus = Integer.valueOf(reportStatusStr);
			criteria.add(Restrictions.eq("reportStatus", reportStatus));
		}
		if(!StringUtils.isEmpty(shieldStatusStr)){
			Integer shieldStatus = Integer.valueOf(shieldStatusStr);
			criteria.add(Restrictions.eq("shieldStatus", shieldStatus));
		}
		if(!StringUtils.isEmpty(tradeStatusStr)){
			Integer tradeStatus = Integer.valueOf(tradeStatusStr);
			criteria.add(Restrictions.eq("tradeStatus", tradeStatus));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("issueTime", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<CommodityInfo> list = this.getHibernateTemplate().findByCriteria(criteria);
		if (list != null && list.size() > 0) {
			count = list.size();
			logger.info("找到("+ list.size() +")个符合查询条件的二货信息");
		} else{
			logger.info("没有找到符合查询条件求的二货信息");
		}
		return count;
	}

	// 5.分页条件查询二货信息记录集合Dao实现类
	@Override
	public List<CommodityInfo> findPageByParams(String id, String oneCategoryIdStr, String twoCategoryIdStr,
			String threeCategoryIdStr, String provinceIdStr, String cityIdStr, String schoolIdStr, String tradeWayStr,
			String reportStatusStr, String shieldStatusStr, String tradeStatusStr, String startDate, String endDate) {
		List<CommodityInfo> result = null;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(CommodityInfo.class);
		if(!StringUtils.isEmpty(id)){
			Integer commodityInfoId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(commodityInfoId));
		}
		if(!StringUtils.isEmpty(oneCategoryIdStr)){
			Integer oneCategoryId = Integer.valueOf(oneCategoryIdStr);
			criteria.add(Restrictions.eq("oneCategoryId", oneCategoryId));
		}
		if(!StringUtils.isEmpty(twoCategoryIdStr)){
			Integer twoCategoryId = Integer.valueOf(twoCategoryIdStr);
			criteria.add(Restrictions.eq("twoCategoryId", twoCategoryId));
		}
		if(!StringUtils.isEmpty(threeCategoryIdStr)){
			Integer threeCategoryId = Integer.valueOf(threeCategoryIdStr);
			criteria.add(Restrictions.eq("threeCategoryId", threeCategoryId));
		}
		if(!StringUtils.isEmpty(provinceIdStr)){
			Integer provinceId = Integer.valueOf(provinceIdStr);
			criteria.add(Restrictions.eq("provinceId", provinceId));
		}
		if(!StringUtils.isEmpty(cityIdStr)){
			Integer cityId = Integer.valueOf(cityIdStr);
			criteria.add(Restrictions.eq("cityId", cityId));
		}
		if(!StringUtils.isEmpty(schoolIdStr)){
			Integer schoolId = Integer.valueOf(schoolIdStr);
			criteria.add(Restrictions.eq("schoolId", schoolId));
		}	
		if(!StringUtils.isEmpty(tradeWayStr)){
			Integer tradeWay = Integer.valueOf(tradeWayStr);
			criteria.add(Restrictions.eq("tradeWay", tradeWay));
		}
		if(!StringUtils.isEmpty(reportStatusStr)){
			Integer reportStatus = Integer.valueOf(reportStatusStr);
			criteria.add(Restrictions.eq("reportStatus", reportStatus));
		}
		if(!StringUtils.isEmpty(shieldStatusStr)){
			Integer shieldStatus = Integer.valueOf(shieldStatusStr);
			criteria.add(Restrictions.eq("shieldStatus", shieldStatus));
		}
		if(!StringUtils.isEmpty(tradeStatusStr)){
			Integer tradeStatus = Integer.valueOf(tradeStatusStr);
			criteria.add(Restrictions.eq("tradeStatus", tradeStatus));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("issueTime", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<CommodityInfo> list = this.getHibernateTemplate().findByCriteria(criteria);
		if (list != null && list.size() > 0) {
			result = list;
			logger.info("找到("+ list.size() +")个符合查询条件的二货信息");
		} else{
			logger.info("没有找到符合查询条件求的二货信息");
		}
		return result;
	}

	// 6.更新二货信息状态Dao实现类
	@Override
	public void updateStatus(Integer id, Integer statusCode) {
		String hql = "from CommodityInfo where id = ?";
		List<CommodityInfo> list = this.getHibernateTemplate().find(hql, id);
		if(list!=null && list.size()>0){
			CommodityInfo commodityInfo = list.get(0);
			commodityInfo.setShieldStatus(statusCode);
			this.getHibernateTemplate().saveOrUpdate(commodityInfo);
		}
	}*/
}
